<?php

declare(strict_types=1);

namespace App\Controller\Admin;


use App\Controller\BaseController;
use App\Helpers\Utils;
use App\Model\Config;
use App\Model\User;

use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\RefreshTokenMiddleware;


/**
 * @AutoController()
 * @Middleware(RefreshTokenMiddleware::class)
 */
class UsersController extends BaseController
{
    public function lists()
    {
        $data = User::where(function ($query){
            if ($this->request->has('phone')){
                $query->where('phone',$this->request->phone);
            }

            if ($this->request->has('sex')){
                $query->where('sex',$this->request->sex);
            }
        })->paginate($this->request->limit);

        return $this->success(['items' => $data->items(),'total' => $data->total()]);
    }



    public function update()
    {
        $post = $this->request->inputs(['nickname','sex']);

        User::where('id',$this->request->input('id'))->update($post);
        return  $this->message('编辑成功');
    }

    //  更新用户状态
    public function userChangeDisplay()
    {
        User::where('id',$this->request->input('id'))->update(['is_display' => (int)$this->request->input('status')]);

        return $this->message('操作成功');
    }


    public function setUserConfig()
    {
        $post = $this->request->all();
        Config::where('name','user_set')->update(['config' => Utils::ArraytoJson($post)]);

        return $this->message('设置成功');
    }

    public function getUserConfig()
    {
        $config = Config::where('name','user_set')->first();

        return $this->success($config->config);
    }

}
